<?php
/**
 * CreateTime: 2024/1/3 11:50
 * User:LinQ
 */

namespace App\Models\System;

use App\Models\BasicModel;

class JobsModel extends BasicModel
{
    protected $table = 'jobs';

    protected $fillable = [
        'jobs_id',
        'com_id',
        'queue_name',
        'jobs_describe',
        'jobs_group_id',
        'sum',
        'flash',
        'fail',
        'jobs_status',
        'status',
        'owner_id',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'jobs_id'  => 'integer' ,
        'com_id'  => 'integer' ,
        'queue_name'  => 'string' ,
        'jobs_describe'=> 'string' ,
        'jobs_group_id'=> 'integer' ,
        'sum'=> 'integer',
        'flash'=> 'integer',
        'fail'=> 'integer',
        'jobs_status' => 'integer',
        'status'=> 'integer',
        'owner_id'=> 'integer',
        'created_at'=> 'string',
        'updated_at'=> 'string',
    ];

    protected $hidden = [
        'status',
        'owner_id',
        'created_at',
        'updated_at',
    ];

    /**
     * 虚假的负载均衡算法
     * @return mixed
     */
    public function calculateSum()
    {
        return $this->selectRaw('`queue_name`, SUM(`sum` - `flash` - `fail`) as total')
            ->groupBy('queue_name')
            ->orderBy('total', 'asc')
            ->value('queue_name');
    }


}